package com.ybfenpan.controller.member.member;


import com.ybfenpan.controller.core.BaseController;
import com.ybfenpan.model.Daili;
import com.ybfenpan.model.Member;
import com.ybfenpan.service.DailiService;
import com.ybfenpan.service.MemberService;
import com.ybfenpan.utils.ContextUtil;
import com.ybfenpan.utils.MapUtil;
import com.ybfenpan.utils.MenuTreeNode;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by Administrator on 2016/3/25.
 */
@Controller
 
public class MapRecommendController extends BaseController {
	@Resource
    private MemberService memberService;
	@Resource
    private DailiService dailiService;
   
	

    @RequestMapping("member/member/tuRecommend.shtml")
    public String tuRecommend(ModelMap model, HttpServletRequest request) throws Exception{
        Map formMap = getParameterMap(request);
        String loginName = MapUtil.getString(formMap, "loginName");
        Member member = ContextUtil.getMember();
        Member self = null;
         
        if(loginName==null || loginName.length()<1){
        	self=memberService.getByKey(member.getId());
			
		}else{
			Member one=memberService.executMemberByLoginName(loginName);
			if(one==null){
				 
				setMessage(model,"该代理商不存在。");
				self=memberService.getByKey(member.getId());
			}else{
				boolean result=false;
				Integer reId = one.getRecommendId();
				Member re = this.memberService.getByKey(reId);
				while(re!=null){
					if(reId.equals(member.getId())){
						result=true;
						break;
					}
					reId=re.getRecommendId();
					re = memberService.getByKey(reId);
				}
				
				
				if(result){
					 
					self=one;
					if(self.getAccountActive().equals("F")){
						setMessage(model,"该代理商未报单。");
						self=memberService.getByKey(member.getId());
					}
				}else{
					 
					setMessage(model,"该代理商不在您下属区域。");
					self=memberService.getByKey(member.getId());
				}
			}
			
			 
			
			 
		}
        Integer id = null;
        if(self!=null){
        	id = self.getId();
		}
       
        request.setAttribute("id",id);
        
        request.setAttribute("loginName",loginName);
       
        return "member/member/tuRecommend";
       
    }
    
    @RequestMapping("member/member/tuRecommendShow.shtml")
    public String tuRecommendShow(HttpServletRequest request) throws Exception{
        Map formMap = getParameterMap(request);
        String loginName = MapUtil.getString(formMap, "loginName");
        Member member = null;
        if(loginName.length()>0){
        	
        	member=memberService.executMemberByLoginName(loginName);
        }
         
        request.setAttribute("member",member);
         
        return "member/member/tuRecommendShow";
       
    }
   
    @RequestMapping("member/member/ajaxTuRecommend.shtml")
    @ResponseBody
    public void ajaxTuRecommendAction(HttpServletRequest request, HttpServletResponse response) {
    	
    	Map formMap = getParameterMap(request);
	    SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    
	    //需要传到页面的值
	    List<MenuTreeNode> menuList = new ArrayList();;
	    int id = MapUtil.getIntValue(formMap,"id");
	    String loginName = MapUtil.getString(formMap,"loginName");
	    Member member = ContextUtil.getMember();
	    
	    
	    if(id<=0){
	    	
	    	Member self=null;
	    	
	    	if(loginName==null || loginName.length()<1){
	    		self=memberService.getByKey(member.getId());
			}else{
				Member one=memberService.executMemberByLoginName(loginName);
				if(one==null){
					self=memberService.getByKey(member.getId());
				}else{
					boolean result=false;
					Integer reId = one.getRecommendId();
					Member re = this.memberService.getByKey(reId);
					while(re!=null){
						if(reId.equals(member.getId())){
							result=true;
							break;
						}
						reId=re.getRecommendId();
						re = memberService.getByKey(reId);
					}
					
					if(result){
						self=one;
					}else{
					 
						self=memberService.getByKey(member.getId());
					}
				}
			}
	    	 
	    	if(self!=null){
	    		
	    		loginName=self.getLoginName();
	    		
				Map map = new HashMap();
		        map.put("accountActive","T");
		        map.put("where"," ( id="+self.getId()+" or recommendId = "+self.getId()+" ) order by id desc ");
		        
		        //条件查询
		        List<Member> personList=memberService.queryForList("select",map);
				 
				//组装菜单节点集合
				
				for(Member person:personList){
					 
					MenuTreeNode node=new MenuTreeNode();
					node.setId(person.getId());
					
					if(person.getRecommendId()!=null){
						node.setFatherId(person.getRecommendId());
					}else{
						node.setFatherId(0);
					}
					
					node.setName("[会员姓名："+person.getPersonName()+"][直推人数:"+person.getRecommendNum()+"][团队人数:"+person.getRecommendTotalNum()+"][报单时间:"+fmt.format(person.getActiveDate())+"]");
					node.setUrl("tuRecommendShow.shtml?loginName="+person.getLoginName());
					node.setTarget("show");
					node.setChildNum(person.getRecommendNum());
					menuList.add(node);
					
					 
				}

			}
	    	
	    }else{
	    	 
			Map map = new HashMap();
	        map.put("accountActive","T");
	        map.put("recommendId",id);
	        map.put("where"," 1=1 order by id desc ");
	        //条件查询
	        List<Member> personList=memberService.queryForList("select",map);
			//组装菜单节点集合
			menuList=new ArrayList<MenuTreeNode>();
			for(Member person:personList){
				
				MenuTreeNode node=new MenuTreeNode();
				node.setId(person.getId());
				
				if(person.getRecommendId()!=null){
					node.setFatherId(person.getRecommendId());
				}else{
					node.setFatherId(0);
				}

				node.setName("[会员姓名："+person.getPersonName()+"][直推人数:"+person.getRecommendNum()+"][团队人数:"+person.getRecommendTotalNum()+"][报单时间:"+fmt.format(person.getActiveDate())+"]");
				
				node.setUrl("tuRecommendShow.shtml?loginName="+person.getLoginName());
				node.setTarget("show");
				node.setChildNum(person.getRecommendNum());
				menuList.add(node);
				
				
			}
	    }
	    HashMap resultformMap = new HashMap();
	    resultformMap.put("menuList", menuList);
	    resultformMap.put("id", id);
	    resultformMap.put("leven", 0);
	    resultformMap.put("loginName", loginName);
	     
	  
	    renderSuccess(response, true, resultformMap);
	    
	   
    }
    

    
    
    
    
    
    
    
    
    
    
    
    
    
    
}
